package zj.com.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.ex.DbException;

import java.util.List;

import zj.com.db.DBUtil;
import zj.com.model.DjcModel;
import zj.com.model.InspectContent;
import zj.com.model.InspectItem;
import zj.com.model.JcxDB;
import zj.com.model.JcxDetailDB;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class JcxAdapter extends BaseAdapter{

    public List<InspectItem> list;
    public Context context;
    public LayoutInflater inflater;

    public JcxAdapter(List<InspectItem> list, Context context){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.adapter_jcx_item, null);
           // viewHolder.type_iv = (ImageView) convertView.findViewById(R.id.type_iv);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            viewHolder.des_tv = (TextView) convertView.findViewById(R.id.des_tv);
            viewHolder.del_iv = (ImageView) convertView.findViewById(R.id.del_iv);
            viewHolder.del_rl = (RelativeLayout) convertView.findViewById(R.id.del_rl);
            viewHolder.tip_tv = (TextView) convertView.findViewById(R.id.tip_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final InspectItem item = list.get(position);

        try {
            List<JcxDetailDB> details =   DBUtil.getInstance(context).getDBManager().selector(JcxDetailDB.class).where("inspectitemcode","=",item.getInspectItemCode()).and("inspectionno","=",item.getInspectionNo()).and("content","!=","").findAll();
            if(details != null && details.size() > 0 ){

                LogUtils.getInstance().error("details  ");
                boolean isDone = false;
                for(JcxDetailDB detailDb:details){
                    LogUtils.getInstance().error("riskname==="+detailDb.getRiskName()+",img=="+detailDb.getImgs());
                    if(!TextUtils.isEmpty(detailDb.getRiskName()) && ! TextUtils.isEmpty(detailDb.getImgs())){
                        isDone = true;
                    }
                }

                if(isDone){
                    viewHolder.tip_tv.setText("");
                    LogUtils.getInstance().error("======完整======");


                }else{
                    viewHolder.tip_tv.setText("[信息不完整]");
                    LogUtils.getInstance().error("======不完整======");

                }
            }else{


                LogUtils.getInstance().error("xxxxxx");
//                List<InspectContent> contents=item.getContents();
//                boolean isOk = false;
//                for(InspectContent content:contents){
//                    LogUtils.getInstance().error("imgs ===="+content.getImgs(),"====riskType==="+content.getRiskTypeName());
//                    if(content.getImgs() != null && content.getRiskTypeName() != null){
//                        isOk = true;
//                    }
//                }
//
//                if(isOk){
//                    viewHolder.tip_tv.setText("");
//                    LogUtils.getInstance().error("======完整======");
//
//
//                }else{
//                    viewHolder.tip_tv.setText("[隐患级别,图片信息不完整]");
//                    LogUtils.getInstance().error("======不完整======");
//
//                }
                viewHolder.tip_tv.setText("[信息不完整]");

            }
        } catch (DbException e) {
            LogUtils.getInstance().error("==db==="+e.toString());
            e.printStackTrace();
        }

        viewHolder.name_tv.setText(item.getInspectItemName());
        viewHolder.des_tv.setText(item.getZyName());

        viewHolder.del_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try
                {
                 JcxDB db =   DBUtil.getInstance(context).getDBManager().selector(JcxDB.class).where("inspectitemcode","=",item.getInspectItemCode()).and("inspectionno", "=", item.getInspectionNo()).findFirst();
                 JcxDetailDB detailDB =   DBUtil.getInstance(context).getDBManager().selector(JcxDetailDB.class).where("inspectitemcode", "=", item.getInspectItemCode()).and("inspectionno", "=", item.getInspectionNo()).findFirst();

                    if(db != null){
                     DBUtil.getInstance(context).getDBManager().delete(db);
                     DBUtil.getInstance(context).getDBManager().delete(detailDB);

                    }
                }catch(Exception e){

                }
                item.getInspectItemID();
                list.remove(item);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }
    private static class ViewHolder {
        ImageView type_iv;
        TextView itemTitle;
        TextView name_tv;
        TextView des_tv;
        ImageView del_iv;
        RelativeLayout del_rl;
        TextView tip_tv;

    }

    public void setDataChange(List<InspectItem> list){

        LogUtils.getInstance().error("===adapter=list==="+list.size());
        this.list = list;
        notifyDataSetChanged();
    }
}
